package com.vhans.db.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vhans.core.domain.CMsg;
import com.vhans.core.utils.BeanCopyUtils;
import com.vhans.core.utils.StringUtils;
import com.vhans.db.mapper.CMsgMapper;
import com.vhans.db.model.dto.ConditionDTO;
import com.vhans.db.model.dto.msg.MsgDTO;
import com.vhans.db.model.vo.msg.MsgInfoVO;
import com.vhans.db.model.vo.msg.MsgLastVO;
import com.vhans.db.model.vo.msg.MsgVO;
import com.vhans.db.service.ICMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户聊天信息Service业务层处理
 *
 * @author vhans
 * @date 2023-05-21
 */
@Service("cMsgServiceImpl")
public class CMsgServiceImpl implements ICMsgService {
    @Autowired
    private CMsgMapper msgMapper;

    @Override
    public MsgLastVO getLastMsg(String roomId) {
        // 查询最后一条消息
        CMsg msg = msgMapper.selectOne(new LambdaQueryWrapper<CMsg>()
                .select(CMsg::getId, CMsg::getRoomId, CMsg::getMsgType, CMsg::getContent, CMsg::getCreateTime)
                .eq(CMsg::getRoomId, roomId)
                .orderByDesc(CMsg::getId)
                .last("LIMIT 1"));
        if (StringUtils.isNull(msg)) {
            // 空消息
            return MsgLastVO.builder()
                    .id(null)
                    .roomId(roomId)
                    .msgType(1)
                    .content("")
                    .createTime(null).build();
        } else {
            return MsgLastVO.builder()
                    .id(msg.getId())
                    .roomId(msg.getRoomId())
                    .msgType(msg.getMsgType())
                    .content(msg.getContent())
                    .createTime(msg.getCreateTime()).build();
        }
    }

    @Override
    public List<MsgVO> getRecentMsg(String roomId) {
        Assert.notNull(roomId, "房间号不能为空");
        String[] ids = roomId.split("_");
        return msgMapper.getRecentMsg(Integer.parseInt(ids[0]), Integer.parseInt(ids[1]));
    }

    @Override
    public void batchReadCMsg(String roomId) {
        // 读取未读消息
        msgMapper.selectList(new LambdaQueryWrapper<CMsg>()
                .select(CMsg::getId)
                .eq(CMsg::getRoomId, roomId)
                .eq(CMsg::getStatus, 2)).forEach(item ->
                msgMapper.updateById(CMsg.builder().id(item.getId()).status(1).build()));
    }

    @Override
    public List<MsgVO> selectMsgVOList(ConditionDTO condition) {
        // 查询用户聊天信息信息
        return msgMapper.selectMsgVOList(condition);
    }

    @Override
    public CMsg insertMsg(MsgDTO cMsg) {
        // 要添加的用户聊天信息
        CMsg newCMsg = BeanCopyUtils.copyBean(cMsg, CMsg.class);
        msgMapper.insert(newCMsg);
        return newCMsg;
    }

    @Override
    public void deleteMsg(Integer msgId) {
        // 删除指定消息
        msgMapper.deleteById(msgId);
    }

    @Override
    public MsgInfoVO selectMsgInfoById(Integer id) {
        // 查询用户聊天信息信息
        MsgInfoVO cMsgInfoVO = msgMapper.selectMsgInfoById(id);
        Assert.notNull(cMsgInfoVO, "没有该用户聊天信息");
        return cMsgInfoVO;
    }

    @Override
    public int deleteMsgByIdIdList(List<Integer> idList) {
        // 删除用户聊天信息
        return msgMapper.deleteBatchIds(idList);
    }
}
